<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="分类" prop="sid">
        <el-select class="!w-160px" v-model="queryParams.sid" clearable placeholder="请选择分类">
          <el-option
            v-for="foodType in foodTypeList"
            :key="foodType.id"
            :label="foodType.name"
            :value="foodType.sid"
          />
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="食物编码" prop="foodId">
        <el-input
          v-model="queryParams.foodId"
          placeholder="请输入食物编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-120px"
        />
      </el-form-item> -->
      <el-form-item label="食物名称" prop="foodName">
        <el-input
          v-model="queryParams.foodName"
          placeholder="请输入食物名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-160px"
        />
      </el-form-item>

      <!-- <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-120px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <!-- <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['school:recipe-food:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button> -->
        <!-- <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['school:recipe-food:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button> -->
        <!-- <el-button
          type="danger"
          plain
          :disabled="isEmpty(checkedIds)"
          @click="handleDeleteBatch"
          v-hasPermi="['school:recipe-food:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <!-- <el-table-column type="selection" width="55" /> -->
      <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
      <el-table-column label="分类编码" align="center" prop="sid" />
      <el-table-column label="食物编码" align="center" prop="foodId" width="120" />
      <el-table-column label="食物名称" align="center" prop="foodName" width="120" />
      <el-table-column label="真实名称" align="center" prop="trueName" />
      <el-table-column label="可食用部分比例（%）" align="center" prop="foodPart" />
      <el-table-column label="水分（g/100g）" align="center" prop="water" />
      <el-table-column label="能量（kcal）" align="center" prop="energyKcal" />
      <el-table-column label="蛋白质（g）" align="center" prop="proteinG" />
      <el-table-column label="脂肪（g）" align="center" prop="fatG" />
      <el-table-column label="碳水化合物（g）" align="center" prop="carbohydrateG" />
      <el-table-column label="膳食纤维（g）" align="center" prop="fiberG" />
      <el-table-column label="维生素A（μg）" align="center" prop="vitaminAUg" />
      <el-table-column label="视黄醇当量（μg）" align="center" prop="retinolEquivalentUg" />
      <el-table-column label="维生素B1（mg）" align="center" prop="vitaminB1Mg" />
      <el-table-column label="维生素B2（mg）" align="center" prop="vitaminB2Mg" />
      <el-table-column label="尼克酸（mg）" align="center" prop="niacinMg" />
      <el-table-column label="维生素C（mg）" align="center" prop="vitaminCMg" />
      <el-table-column label="维生素E（mg）" align="center" prop="vitaminEMg" />
      <el-table-column label="β-胡萝卜素（μg）" align="center" prop="betaCaroteneUg" />
      <el-table-column label="钙（mg）" align="center" prop="calciumMg" />
      <el-table-column label="磷（mg）" align="center" prop="phosphorusMg" />
      <el-table-column label="钾（mg）" align="center" prop="potassiumMg" />
      <el-table-column label="钠（mg）" align="center" prop="sodiumMg" />
      <el-table-column label="镁（mg）" align="center" prop="magnesiumMg" />
      <el-table-column label="铁（mg）" align="center" prop="ironMg" />
      <el-table-column label="锌（mg）" align="center" prop="zincMg" />
      <el-table-column label="硒（mg）" align="center" prop="seleniumMg" />
      <el-table-column label="铜（mg）" align="center" prop="copperMg" />
      <el-table-column label="锰（mg）" align="center" prop="manganeseMg" />
      <el-table-column label="碘（μg）" align="center" prop="iodineUg" />
      <!-- <el-table-column label="排序号" align="center" prop="sort" /> -->
      <!-- <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      /> -->
      <!-- <el-table-column label="操作" align="center" fixed="right" width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['school:recipe-food:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['school:recipe-food:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <RecipeFoodForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { RecipeFoodApi, RecipeFood } from '@/api/school/recipe/food'
import RecipeFoodForm from './RecipeFoodForm.vue'
import { RecipeFoodTypeApi, RecipeFoodType } from '@/api/school/recipe/foodType'

/** 食物管理 列表 */
defineOptions({ name: 'RecipeFood' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<RecipeFood[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  sid: undefined,
  foodId: undefined,
  foodName: undefined,
  trueName: undefined,
  status: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const foodTypeList = ref([] as RecipeFoodType[]) //食物分类列表

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RecipeFoodApi.getRecipeFoodPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await RecipeFoodApi.deleteRecipeFood(id)
    message.success(t('common.delSuccess'))
    // currentRow.value = {}
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除食物管理 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await RecipeFoodApi.deleteRecipeFoodList(checkedIds.value)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: RecipeFood[]) => {
  checkedIds.value = records.map((item) => item.id)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await RecipeFoodApi.exportRecipeFood(queryParams)
    download.excel(data, '食物管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  getList()

  //查询食物分类列表
  foodTypeList.value = await RecipeFoodTypeApi.getSimpleRecipeFoodType()
})
</script>
